package com.fqxiny.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fqxiny.lease.common.result.Result;
import com.fqxiny.lease.model.entity.*;
import com.fqxiny.lease.model.enums.ItemType;
import com.fqxiny.lease.web.admin.service.*;
import com.fqxiny.lease.web.admin.mapper.RoomInfoMapper;
import com.fqxiny.lease.web.admin.vo.attr.AttrValueVo;
import com.fqxiny.lease.web.admin.vo.graph.GraphVo;
import com.fqxiny.lease.web.admin.vo.room.RoomDetailVo;
import com.fqxiny.lease.web.admin.vo.room.RoomItemVo;
import com.fqxiny.lease.web.admin.vo.room.RoomQueryVo;
import com.fqxiny.lease.web.admin.vo.room.RoomSubmitVo;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
* @author 25312
* @description 针对表【room_info(房间信息表)】的数据库操作Service实现
* @createDate 2025-03-25 19:55:24
*/
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
    implements RoomInfoService{
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private AttrValueService attrValueService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private FacilityInfoService facilityInfoService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private LabelInfoService labelInfoService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private PaymentTypeService paymentTypeService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    @Autowired
    @Lazy
    private ApartmentInfoService apartmentInfoService;

    @Autowired
    private LeaseTermService leaseTermService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Resource
    private RoomInfoMapper roomInfoMapper;
    @Override
    public RoomDetailVo getDetailById(Long id) {
        RoomInfo roomInfo = getById(id);
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        roomDetailVo.setRoomNumber(roomInfo.getRoomNumber());
        roomDetailVo.setRent(roomInfo.getRent());
        roomDetailVo.setApartmentId(roomInfo.getApartmentId());
        roomDetailVo.setIsRelease(roomInfo.getIsRelease());

        roomDetailVo.setApartmentInfo(apartmentInfoService.getById(roomInfo.getApartmentId()));
        roomDetailVo.setGraphVoList(graphInfoService.listByRoomId(id));
        //todo : 完善关系表为null逻辑处理 , 避免 in（）语句为空
        roomDetailVo.setAttrValueVoList(attrValueService.listByProvidedIds(roomAttrValueService.listByRoomIdLongs(id)));

        roomDetailVo.setFacilityInfoList(facilityInfoService.getBaseMapper().selectBatchIds(roomFacilityService.listByRoomId(id)));
        roomDetailVo.setLabelInfoList(labelInfoService.getBaseMapper().selectBatchIds(roomLabelService.listByRoomId(id)));
        roomDetailVo.setPaymentTypeList(paymentTypeService.getBaseMapper().selectBatchIds(roomPaymentTypeService.listByRoomId(id)));
        roomDetailVo.setLeaseTermList(leaseTermService.getBaseMapper().selectBatchIds(roomLeaseTermService.listByRoomId(id)));
        return roomDetailVo;
    }

    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        boolean update = roomSubmitVo.getId()!=null;
        if(update){
            //1.删除graph info
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,roomSubmitVo.getId());
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);
            //2.删除roomLeaseTermList
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,roomSubmitVo.getId());
            roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
            //3.删除roomPaymentTypeList
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,roomSubmitVo.getId());
            roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
            //4.删除roomAttrValueList
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,roomSubmitVo.getId());
            roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);
            //5.删除roomLabelList
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,roomSubmitVo.getId());
            roomLabelService.remove(roomLabelLambdaQueryWrapper);
            //6.删除roomFacilityList
            LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,roomSubmitVo.getId());
            roomFacilityService.remove(roomFacilityLambdaQueryWrapper);
        }
        // todo :!null 判定
        // 1. save room info
        RoomInfo roomInfo = new RoomInfo();
        roomInfo.setRoomNumber(roomSubmitVo.getRoomNumber());
        roomInfo.setRent(roomSubmitVo.getRent());
        roomInfo.setApartmentId(roomSubmitVo.getApartmentId());
        roomInfo.setIsRelease(roomSubmitVo.getIsRelease());
        saveOrUpdate(roomInfo);

        // 2. save graph info
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfos = new ArrayList<>(10);
        for (GraphVo graphVo : graphVoList) {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setName(graphVo.getName());
            graphInfo.setUrl(graphVo.getUrl());
            graphInfo.setItemId(roomSubmitVo.getId());
            graphInfo.setItemType(ItemType.ROOM);
            graphInfos.add(graphInfo);
        }
        graphInfoService.saveBatch(graphInfos);

        // 3. save lease term
        List<Long> leaseTermList = roomSubmitVo.getLeaseTermIds();
        List<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>(5);
        for (Long l : leaseTermList) {
            RoomLeaseTerm roomLeaseTerm = new RoomLeaseTerm();
            roomLeaseTerm.setRoomId(roomSubmitVo.getId());
            roomLeaseTerm.setLeaseTermId(l);
        }
        roomLeaseTermService.saveBatch(roomLeaseTerms);
        //4. save payment type
        List<Long> paymentTypeList = roomSubmitVo.getPaymentTypeIds();
        List<RoomPaymentType> roomPaymentTypes = new ArrayList<>(5);
        for (Long l : paymentTypeList) {
            RoomPaymentType roomPaymentType = RoomPaymentType.builder().build();
            roomPaymentType.setRoomId(roomSubmitVo.getId());
            roomPaymentType.setPaymentTypeId(l);
        }
        roomPaymentTypeService.saveBatch(roomPaymentTypes);
        //5. save attr value
        List<Long> attrValueVoList = roomSubmitVo.getAttrValueIds();
        List<RoomAttrValue> roomAttrValues = new ArrayList<>(5);
        for (Long l : attrValueVoList) {
            RoomAttrValue roomAttrValue = RoomAttrValue.builder().build();
            roomAttrValue.setRoomId(roomSubmitVo.getId());
            roomAttrValue.setAttrValueId(l);
        }
        roomAttrValueService.saveBatch(roomAttrValues);
        //6. save label
        List<Long> labelInfoList = roomSubmitVo.getLabelInfoIds();
        List<RoomLabel> roomLabels = new ArrayList<>(5);
        for (Long l : labelInfoList) {
            RoomLabel roomLabel = RoomLabel.builder().build();
            roomLabel.setRoomId(roomSubmitVo.getId());
            roomLabel.setLabelId(l);
        }
        roomLabelService.saveBatch(roomLabels);
        //7. save facility
        List<Long> facilityInfoList = roomSubmitVo.getFacilityInfoIds();
        List<RoomFacility> roomFacilities = new ArrayList<>(5);
        for (Long l : facilityInfoList) {
            RoomFacility roomFacility = RoomFacility.builder().build();
            roomFacility.setRoomId(roomSubmitVo.getId());
            roomFacility.setFacilityId(l);
        }
        roomFacilityService.saveBatch(roomFacilities);
    }

    @Override
    public IPage<RoomItemVo> pageItem(IPage<RoomItemVo> page, RoomQueryVo roomQueryVo) {

        return roomInfoMapper.pageItem(page, roomQueryVo);
    }

    @Override
    public void removeRoomById(Long id) {
        //1.删除graphInfoList
        LambdaQueryWrapper<GraphInfo> graphQueryWrapper = new LambdaQueryWrapper<>();
        graphQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        graphQueryWrapper.eq(GraphInfo::getItemId, id);
        graphInfoService.remove(graphQueryWrapper);

        //2.删除attrValueList
        LambdaQueryWrapper<RoomAttrValue> attrQueryWrapper = new LambdaQueryWrapper<>();
        attrQueryWrapper.eq(RoomAttrValue::getRoomId, id);
        roomAttrValueService.remove(attrQueryWrapper);

        //3.删除facilityInfoList
        LambdaQueryWrapper<RoomFacility> facilityQueryWrapper = new LambdaQueryWrapper<>();
        facilityQueryWrapper.eq(RoomFacility::getRoomId, id);
        roomFacilityService.remove(facilityQueryWrapper);

        //4.删除labelInfoList
        LambdaQueryWrapper<RoomLabel> labelQueryWrapper = new LambdaQueryWrapper<>();
        labelQueryWrapper.eq(RoomLabel::getRoomId, id);
        roomLabelService.remove(labelQueryWrapper);

        //5.删除paymentTypeList
        LambdaQueryWrapper<RoomPaymentType> paymentQueryWrapper = new LambdaQueryWrapper<>();
        paymentQueryWrapper.eq(RoomPaymentType::getRoomId, id);
        roomPaymentTypeService.remove(paymentQueryWrapper);

        //6.删除leaseTermList
        LambdaQueryWrapper<RoomLeaseTerm> termQueryWrapper = new LambdaQueryWrapper<>();
        termQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
        roomLeaseTermService.remove(termQueryWrapper);

        //7.删除RoomInfo
        this.removeById(id);
    }
}




